/*
 * @Author: meetqy
 * @since: 2019-08-06 11:35:23
 * @lastTime: 2019-11-22 15:23:58
 * @LastEditors: meetqy
 */

import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:fluttertz/pages/toolbar/addjt/index.dart';
import 'package:fluttertz/pages/toolbar/home/index.dart';
import 'package:fluttertz/pages/toolbar/mine/index.dart';
import 'package:fluttertz/utils/Icon.dart';

class Toolbar extends StatefulWidget {
  final String routeName;
  final Object arguments;

  // 初始化所有的toolbar页面
  static Home _home = Home();
  static Addjt _addjt = Addjt();
  static Mine _mine = Mine();

  /// 所有toolbar页面
  final Map<int, Map> pages = {
    0: _createPage(_home, appbar: _home.getAppBar(), routeName: '/home'),
    1: _createPage(_addjt, appbar: _addjt.getAppBar(), routeName: '/addjt'),
    2: _createPage(_mine, appbar: _mine.getAppBar(), routeName: '/mine')
  };

  /// 创建页面map
  /// ```
  /// @param {Widget} page - 页面
  /// @param {Appbar} appbar - 当前页面是否显示appbar 默认为true
  /// ```
  static Map _createPage(Widget page, {AppBar appbar, String routeName}) {
    return {"widget": page, "appbar": appbar, "routeName": routeName};
  }

  static Toolbar _singleton;

  Toolbar.singleton({this.routeName, this.arguments});

  factory Toolbar({String routeName, Object arguments}) {
    if (_singleton == null) {
      _singleton = Toolbar.singleton(
        routeName: routeName,
        arguments: arguments,
      );
    }

    return _singleton;
  }

  // 通过 routeName 获取对应页面的索引
  getPageIndex(routeName) {
    switch (routeName) {
      case '/addjt':
        return 1;
      case '/mine':
        return 2;
      default:
        return 0;
    }
  }

  _NavigationState createState() => _NavigationState();
}

class _NavigationState extends State<Toolbar> {
  static int currentIndex = 0;

  @override
  void initState() {
    super.initState();

    Future.delayed(Duration.zero, () {
      String routeName = ModalRoute.of(context).settings.name;
      setState(() {
        currentIndex = widget.getPageIndex(routeName);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    Map page = widget.pages[currentIndex];
    return Scaffold(
        appBar: page['appbar'],
        body: Container(color: hex('#fff'), child: page['widget']),
        bottomNavigationBar: Theme(
            data: ThemeData(
                highlightColor: Colors.transparent,
                splashColor: Colors.transparent),
            child: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              items: [
                BottomNavigationBarItem(
                  icon: iconjt(),
                  title: Text('機台'),
                ),
                BottomNavigationBarItem(
                  icon: iconxz(),
                  title: Text('新增'),
                ),
                BottomNavigationBarItem(
                  icon: iconzh(),
                  title: Text('賬號'),
                ),
              ],
              unselectedFontSize: 10,
              // 未选中字体大小
              selectedFontSize: 13,
              // 选中字体大小
              selectedItemColor: rgba(113, 163, 224, 1),
              // 选中字体颜色
              currentIndex: currentIndex,
              onTap: (index) {
                setState(() {
                  currentIndex = index;
                });
              },
            )));
  }
}
